﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace XMLEditor
{
    class XMLEditor
    {
        static void Main(string[] args)
        {
            //Dictionary<string, int> artistAlbums = new Dictionary<string, int>();
            //XmlDocument doc = new XmlDocument();
            //doc.Load("../../../Catalogue.xml");
            //XmlNode rootNode = doc.DocumentElement;

            //foreach (XmlNode node in rootNode.ChildNodes)
            //{
            //    string artist = node["artist"].InnerText;
            //    if (!artistAlbums.ContainsKey(artist))
            //    {
            //        artistAlbums[artist] = 0;
            //    }
            //    artistAlbums[artist]++;
            //}
            //foreach (var item in artistAlbums)
            //{
            //    Console.WriteLine("Artist: {0}\nNumber of Albums: {1}", item.Key, item.Value);
            //}

            //XmlDocument xmlDoc = new XmlDocument();
            //xmlDoc.Load("../../../Catalogue.xml");
            //string xPathQuery = "/catalogue/album";
            //Dictionary<string, int> artistAlbums = new Dictionary<string, int>();
            //XmlNodeList artistsList = xmlDoc.SelectNodes(xPathQuery);

            //foreach (XmlNode beerNode in artistsList)
            //{
            //    string artist = beerNode.SelectSingleNode("artist").InnerText.ToString();
            //    if (!artistAlbums.ContainsKey(artist))
            //    {
            //        artistAlbums[artist] = 0;
            //    }
            //    artistAlbums[artist]++;
            //}

            //foreach (var item in artistAlbums)
            //{
            //    Console.WriteLine("Artist: {0}\nNumber of Albums: {1}", item.Key, item.Value);
            //}

            //XmlDocument doc = new XmlDocument();
            //doc.Load("../../../Catalogue.xml");
            //XmlNode rootNode = doc.DocumentElement;
            //List<XmlNode> cheapNodes = new List<XmlNode>();

            //for (int i = 0; i < rootNode.ChildNodes.Count; i++)
            //{
            //    XmlNode node = rootNode.ChildNodes[i];
            //    double price = double.Parse(node["price"].InnerText);
            //    if (price>20.00)
            //    {
            //        rootNode.RemoveChild(node);
            //        i--;
            //    }
            //}
            //doc.Save("../../../CheapCatalogue.xml");

            //using (XmlReader reader = XmlReader.Create("../../../Catalogue.xml"))
            //{
            //    while (reader.Read())
            //    {
            //        if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "songs"))
            //        {
            //            Console.Write(reader.ReadElementString());
            //        }
            //    }
            //}

            //XDocument xmlDoc = XDocument.Load("../../../Catalogue.xml");
            //var songs =
            //  from song in xmlDoc.Descendants("album")
            //  select  song.Element("songs").Value;


            //foreach (var song in songs)
            //{
            //    Console.WriteLine(song);
            //}

            string fileName = "../../../persons.xml";
            Encoding encoding = Encoding.GetEncoding("windows-1251");
            using (XmlTextWriter writer = new XmlTextWriter(fileName, encoding))
            {
                writer.Formatting = Formatting.Indented;
                writer.IndentChar = '\t';
                writer.Indentation = 1;

                writer.WriteStartDocument();
                writer.WriteStartElement("library");
                writer.WriteAttributeString("name", "My Library");
                WritePerson(writer, "Stanislav Savov", "Ovcha Kupel, Sofia", "155-615-484-4");
                WritePerson(writer, "Ivan Petrov", "Plovdiv", "954-775-305-3");
                WritePerson(writer, "Georgi Cheshmedjiev", "Varna", "155-615-551-4");
                writer.WriteEndDocument();
            }
            Console.WriteLine("Document {0} created.", fileName);
        }

        private static void WritePerson(XmlTextWriter writer, string name, string adress, string phone)
        {
            writer.WriteStartElement("Person");
            writer.WriteElementString("Name", name);
            writer.WriteElementString("Adress", adress);
            writer.WriteElementString("Phone", phone);
            writer.WriteEndElement();
        }
    }
}
